package cn.neusoft.luofa603.yuejian_29.activity;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.neusoft.luofa603.yuejian_29.fragment.ClubFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.MyFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.ShareFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.TrainingFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.UnionFragment;
import cn.neusoft.luofa603.yuejian_29.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private LinearLayout mTabtraining;
    private LinearLayout mTabclub;
    private LinearLayout mTabun;
    private LinearLayout mTabshare;
    private LinearLayout mTabmy;

    private ImageButton mImgtraining;
    private ImageButton mImgclub;
    private ImageButton mImgun;
    private ImageButton mImgshare;
    private ImageButton mImgmy;

    private TrainingFragment mTrainingFragment;
    private ClubFragment mClubFragment;
    private UnionFragment mUnionFragment;
    private ShareFragment mShareFragment;
    private MyFragment mMyFragment;
    private TextView topTitle;
    private ImageView search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();//初始化事件
        setSelect(0);

    }

    private void initView()
    {
        topTitle = (TextView) findViewById(R.id.top_title);
        search=(ImageView)findViewById(R.id.search);
        mTabtraining = (LinearLayout) findViewById(R.id.id_px);
        mTabclub = (LinearLayout) findViewById(R.id.id_club);
        mTabun = (LinearLayout) findViewById(R.id.id_un);
        mTabshare = (LinearLayout) findViewById(R.id.id_share);
        mTabmy = (LinearLayout) findViewById(R.id.id_wode);

        mImgtraining = (ImageButton) findViewById(R.id.id_px_img);
        mImgclub = (ImageButton) findViewById(R.id.id_club_img);
        mImgun = (ImageButton) findViewById(R.id.id_un_img);
        mImgshare = (ImageButton) findViewById(R.id.id_share_img);
        mImgmy = (ImageButton) findViewById(R.id.id_wode_img);
    }

    private void initEvent()
    {
        mTabun.setOnClickListener(this);
        mTabclub.setOnClickListener(this);
        mTabtraining.setOnClickListener(this);
        mTabshare.setOnClickListener(this);
        mTabmy.setOnClickListener(this);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTrainingFragment == null)
                {
                    mTrainingFragment = new TrainingFragment();
                    transaction.add(R.id.id_content,mTrainingFragment);
                }
                topTitle.setText("体育培训");
                search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this,TrainSearchActivity.class);
                        startActivity(intent);
                    }
                });
                transaction.show(mTrainingFragment);
                mImgtraining.setImageResource(R.drawable.ic_peixun);
                break;
            case 1:
                if (mClubFragment == null)
                {
                    mClubFragment = new ClubFragment();
                    transaction.add(R.id.id_content, mClubFragment);
                }
                topTitle.setText("俱乐部");
                search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this,ClubSearchActivity.class);
                        startActivity(intent);
                    }
                });
                transaction.show(mClubFragment);
                mImgclub.setImageResource(R.drawable.ic_club);
                break;
            case 2:
                if (mUnionFragment == null)
                {
                   mUnionFragment = new UnionFragment();
                    transaction.add(R.id.id_content, mUnionFragment);
                }
                topTitle.setText("约健体育联盟");
                transaction.show(mUnionFragment);
                mImgun.setImageResource(R.drawable.ic_un);
                break;
            case 3:
                if (mShareFragment == null)
                {
                    mShareFragment = new ShareFragment();
                    transaction.add(R.id.id_content,mShareFragment);
                }
                topTitle.setText("晒图");
                transaction.show(mShareFragment);
                mImgshare.setImageResource(R.drawable.ic_dongtai);
                break;

            case 4:
                if (mMyFragment == null)
                {
                   mMyFragment = new MyFragment();
                    transaction.add(R.id.id_content,mMyFragment);
                }
                topTitle.setText("个人中心");
                transaction.show(mMyFragment);
                mImgmy.setImageResource(R.drawable.ic_wode);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTrainingFragment != null)
        {
            transaction.hide(mTrainingFragment);
        }
        if (mClubFragment != null)
        {
            transaction.hide(mClubFragment);
        }
        if (mUnionFragment != null)
        {
            transaction.hide(mUnionFragment);
        }
        if (mShareFragment != null)
        {
            transaction.hide(mShareFragment);
        }
        if (mMyFragment != null)
        {
            transaction.hide(mMyFragment);
        }
    }



    @Override
    public void onClick(View v) {

        resetImgs();
        switch (v.getId())
        {
            case R.id.id_px:
                setSelect(0);
                break;
            case R.id.id_club:
                setSelect(1);
                break;
            case R.id.id_un:
                setSelect(2);
                break;
            case R.id.id_share:
                setSelect(3);
                break;
            case R.id.id_wode:
                setSelect(4);
                break;


            default:
                break;
        }

    }

    private void resetImgs()
    {
        mImgtraining.setImageResource(R.drawable.ic_pxnormal);
        mImgclub.setImageResource(R.drawable.ic_clubnormal);
        mImgun.setImageResource(R.drawable.ic_unnormal);
        mImgshare.setImageResource(R.drawable.ic_dongtainormal);
        mImgmy.setImageResource(R.drawable.ic_wodenormal);
    }
}
